﻿#region 文件信息

/*----------------------------------------------------------------
//
// 文件名称：
// 文件功能描述：
// 设计要求：
//
// 文 件 名：    TreeBuilderByFullLoad
// 创建者：      杨程
// 创建日期：	    2022/11/21 16:51:21

//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.GeneralTree;

/// <summary>
/// 全量加载树数据
/// </summary>
public class TreeBuilderByFullLoad<TSource> : TreeBuilderBase<TSource>
        where TSource : class, ITreeNode
{
    /// <summary>
    /// 加载全数据节点
    /// </summary>
    /// <param name="nodes">节点数据集合</param>
    public void LoadFullNodes(IEnumerable<TSource> nodes)
    {
        BindingTreeItems.Clear();
        _sourceTreeNodes.Clear();
        if (nodes != null && nodes.Count() > 0)
        {
            var rootNodes = GeneralTreeItemsManager<TSource>.BuildTree(nodes);
            _sourceTreeNodes.AddRange(GeneralTreeItemsManager<TSource>.BuildContainer(nodes));
            int tmpIndex = 1;
            foreach (var item in rootNodes)
            {
                BindingTreeItems.Add(item);
                item.RefreshLevel(tmpIndex);
                tmpIndex++;
            }
        }
    }



    /// <summary>
    /// 当前展开的层级
    /// </summary>
    private int? _lastExpandLevel;
    /// <summary>
    /// 最后一次展开状态
    /// </summary>
    private bool _lastExpandState;


    private ITreePath _possionNode;

    public ITreePath PossionNode
    {
        get
        {
            return _possionNode;
        }
    }

    /// <summary>
    /// 展开对应的层级节点
    /// </summary>
    /// <param name="level">层级</param>
    public void ExpandNodeByLevelNum(int level)
    {
        if (_lastExpandLevel.HasValue && level == _lastExpandLevel.Value)
            _lastExpandState = !_lastExpandState;
        else
            _lastExpandState = true;

        _lastExpandLevel = level;

        foreach (var item in BindingTreeItems)
        {
            item.ExpandNodeByLevelNumber(level, _lastExpandState);
        }
    }

    public async void ExpandNodeByLevelNumAsync(int level, bool? isLoading)
    {
        if (isLoading.HasValue) isLoading = true;

        await Task.Run(() =>
        {
            ExpandNodeByLevelNum(level);
        });

        if (isLoading.HasValue) isLoading = false;
    }
}